package com.cms.hotfix.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * AUTHOR：YF
 * DATE: 2024/1/8 0008
 * DESCRIBE:
 *
 * @author GW00286792
 */
public class ReflexJavaUtils {


    public static Method findMethod(Object obj, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException {
        for (Class<?> clazz = obj.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
            try {
                Method declaredMethod = clazz.getDeclaredMethod(methodName, parameterTypes);
                if (!declaredMethod.isAccessible()) {
                    declaredMethod.setAccessible(true);
                }
                return declaredMethod;
            } catch (NoSuchMethodException e) {

            }
        }
        throw new NoSuchMethodException("Method " + methodName + " is not find in" + obj.getClass());
    }

    public static Field findField(Object obj, String fieldName) throws NoSuchFieldException {
        for (Class<?> clazz = obj.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
            try {
                Field field = clazz.getDeclaredField(fieldName);
                if (!field.isAccessible()) {
                    field.setAccessible(true);
                }
                return field;
            } catch (NoSuchFieldException e) {

            }
        }
        throw new NoSuchFieldException("Field " + fieldName + " not find in " + obj.getClass());

    }

}
